home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 02 - 1986 / 02.09 Sep 86.sit / 02.09 Sep 86 / forth source / Structures < prev   
Encoding:
Text File  |  1986-06-26  |  8.0 KB  |  1 lines  |  [BLKS/M4TH]

  1. Structures                                        ( 062686 jl ) ----------                                                      Adding a structure compiler to Forth. JL 26.6.86.                                                                               This file defines a Pascal-like 'record' structure;             a record is a template for instances of the structure.          ( Example )                                                     :record a                                                           >long field1                                                    >word field2                                                ;record                                                         ( then do )                                                     myrec r1 ( this creates an instance r1 of myrec whose fields              may be accessed through myrec ^ field1 etc..)         ( for 'late binding' usage, the word ^field is provided)                                                                        ( structures, load block )                        ( 062686 jl )                                                                 decimal                                                         2 7 thru                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ( do.record, creating one instance of a record )  ( 062686 jl )                                                                 : do.record  ( addr of master | -- )                                create  dup ,                                                       begin dup c@ dup while ( not zero, i.e. end)                        1+ =cells 4+ + ( next field in template )                   repeat                                                      drop 2+ w@ ( length stored here ) allot                                                                                         does>  ( nothing special )                                  ;                                                                                                                                                                                                                                                                                                                                                                                               ( :record ;record and friends)                    ( 062686 jl )                                                                 :  :record  create 13579 4 does> do.record ;                                                                                    :  ;record  2 needed                                                0 w, ( end of list) w, ( total length )                         13579 = 0= abort" ;record without :record"                  ;                                                                                                                               :  put.fieldname                                                    32 word here over c@ 1+ dup =cells allot cmove ;                                                                                                                                                                                                                                                                                                                                            ( field defining words )                          ( 062686 jl )                                                                 : >long ( addr | addr+4)                                                put.fieldname dup w, 4 w, 4+ ;                          : >word ( addr | addr+2)                                                put.fieldname dup w, 2 w, 2+ ;                          : >byte ( addr | addr+1)                                                put.fieldname dup w, 1 w, 1+ ;                          : >bytes ( addr \ n | addr+n )                                          put.fieldname over w, dup w, + ;                                                                                                                                                                                                                                                                                                                                                                                                                        ( ^field, addressing a field within a record )    ( 062686 jl )                                                                 : ^field ( addr name | address of field )                           over @ ( addr name master )                                         begin 2dup -string while ( no match )                               dup c@ 6+ =cells +                                              dup c@ 0= ( end of list )                                           abort" RECORD- specified field does not exist"          repeat                                                      ( match found )                                                 dup c@ 1+ =cells + w@ ( start within record )                   swap drop   +  ( address of field )                         ;                                                                                                                                                                                                                                                               ( ^ , <[ , ]> )                                   ( 062686 jl )                                                                 : ^   32 word ^field ;                                                                                                                                                                          : <[ [compile] [ ; immediate                                    : ]> ] [compile] literal ; immediate                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ( example of a record structure )                 ( 062686 jl ) :record testrec                                                     >long date                                                      >long time                                                      >byte flag                                                      >word counts                                                 30 >bytes description                                          ;record                                                                                                                         testrec r1                                                      testrec r2